#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code:utf-8
#
# Copyright(C) 2011 SupDo.com
# Licensed under the GUN License, Version 3.0 (the "License");
# 
# File:        include
# Author:      KuKei
# Create Date: 2011-04-27
# Description: 页面功能处理视图
# Modify Date: 2011-04-27

import os
from django.http import HttpResponse
import Image, ImageDraw, ImageFont, hashlib, random, cStringIO
from sd.cms.lib import Params
from sd import settings

def safecode(request, **args):
    im = Image.new('RGBA',(80,26),(50,50,50,50))
    draw = ImageDraw.Draw(im)
    #字体颜色
    colorlist = ['white','yellow','red','green']
    #4位随机数
    randstr = str(random.randint(1000,9999))
    filename = os.path.dirname(__file__).replace('\\','/')+"/mvboli.TTF"
    leftpos = 2
    index = 0
    for sc in randstr:
        draw.text((leftpos+index*19,-3), sc, font=ImageFont.truetype(filename, random.randrange(18,25)), fill=random.choice(colorlist))
        index += 1
    del draw
    request.session[Params.SAFECODE] = hashlib.sha1(randstr).hexdigest()
    buf = cStringIO.StringIO()
    im.save(buf, 'gif')
    return HttpResponse(buf.getvalue(),'image/gif')